package hu.veilingsite.gui;

import java.sql.Date;
import java.util.ArrayList;
import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.client.Systeem;
import hu.veilingsite.shared.Auction;
import hu.veilingsite.shared.Bid;
import hu.veilingsite.shared.Category;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;

public class EditAuction {
	private FlexTable table;
	private ListBox LB_category;
	private Button BTN_editAuction;
	private TextArea TA_description;
	private TextBox TB_title, TB_credits, TB_dag, TB_maand, TB_jaar;
	private Label LB_title, LB_credits, LB_datum, LB_description, LBL_category, LB_visible;
	private CheckBox CB_visible;
	private ArrayList<Category> categories;
	private Auction veiling;
	private CellTable<Bid> tableBids = new CellTable<Bid>();
	private final ServiceAsync myService = GWT.create(Service.class);
	
	final AsyncCallback<ArrayList<Bid>> getBidsCallback = new AsyncCallback<ArrayList<Bid>>() {
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("Fout tijdens het ophalen van biedingen.");
		}
		@Override
		public void onSuccess(ArrayList<Bid> result) {
			tableBids.setRowCount(result.size(),false);
			tableBids.setRowData(result);
		}
	};
	
	public EditAuction(){

	}

	AsyncCallback<ArrayList<Category>> getList = new AsyncCallback<ArrayList<Category>>(){
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("er is een fout opgetreden. wacht aub tot deze door onze admin is opgelost");
		}
		@Override
		public void onSuccess(ArrayList<Category> result) {
			categories = result;

			LB_category = new ListBox();
			for(Category cat : categories){
				LB_category.addItem(cat.getTitle(), "" + cat.getId());
			}
			LB_category.setSelectedIndex(veiling.getCategory_id());

			HorizontalPanel p4 = new HorizontalPanel();

			p4.add(TB_dag);
			p4.add(TB_maand);
			p4.add(TB_jaar);

			table.setWidget(0, 0, LBL_category);
			table.setWidget(0, 1, LB_category);
			table.setWidget(1, 0, LB_title);
			table.setWidget(1, 1, TB_title);
			table.setWidget(2, 0, LB_description);
			table.setWidget(2, 1, TA_description);
			table.setWidget(3, 0, LB_credits);
			table.setWidget(3, 1, TB_credits);
			table.setWidget(4, 0, LB_datum);
			table.setWidget(4, 1, p4);
			if(Systeem.ingelogd.isIsadmin()){
				table.setWidget(5, 0, LB_visible);
				table.setWidget(5, 1, CB_visible);
				table.setWidget(6, 1, BTN_editAuction);
			}
			else{
				table.setWidget(5, 1, BTN_editAuction);
			}
			HorizontalPanel p = new HorizontalPanel();
			p.add(table);
			//Bied historie
			TextColumn<Bid> uidColumn = new TextColumn<Bid>() {
				@Override
				public String getValue(Bid bid) {
					return ""+bid.getUser_id();
				}
			};
			TextColumn<Bid> dateColumn = new TextColumn<Bid>() {
				@Override
				public String getValue(Bid bid) {
					return bid.getDate().toString();
				}
			};
			TextColumn<Bid> creditColumn = new TextColumn<Bid>() {
				@Override
				public String getValue(Bid bid) {
					return ""+bid.getCredits();
				}
			};
			
			tableBids.setStyleName("tableCategories");
			tableBids.addColumn(uidColumn);
			tableBids.addColumn(dateColumn);
			tableBids.addColumn(creditColumn);
			myService.getBidsFromAuction(veiling.getId(),getBidsCallback);
			p.add(tableBids);
			RootPanel main = RootPanel.get("main");
			main.clear();
			main.add(p);
		}
	};

	AsyncCallback<Auction> getAuction = new AsyncCallback<Auction>(){
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("er is een fout opgetreden. wacht aub tot deze door onze admin is opgelost");
		}
		@Override
		public void onSuccess(Auction result) {
			veiling = result;
			TB_title.setText(result.getTitle());
			TB_credits.setText("" + result.getStartPrice());
			TB_dag.setText("" + result.getEndDate().getDay());
			TB_maand.setText("" + (result.getEndDate().getMonth() + 1));
			TB_jaar.setText("" + (result.getEndDate().getYear() + 1900));
			TA_description.setText(result.getDescription());
			if(result.isVisible()){
				CB_visible.setValue(true);
			}
			else{
				CB_visible.setValue(false);
			}
			myService.getCategories(getList);
		}
	};

	AsyncCallback<Void> opslaan = new AsyncCallback<Void>(){
		@Override
		public void onFailure(Throwable caught) {
			Window.alert("er is een fout opgetreden. wacht aub tot deze door onze admin is opgelost");
		}
		@Override
		public void onSuccess(Void result) {
			RootPanel.get("main").clear();
			new ViewAuction(veiling.getId()).createPage();
		}
	};

	public void createPage(final int id){
		//initialiseren van de opjecten
		table = new FlexTable();

		LB_title = new Label("Titel");
		LB_credits = new Label("Aantal credits");
		LB_datum = new Label("Einddatum");
		LB_description = new Label("Omschrijving");
		LBL_category = new Label("Categorie");
		LB_visible = new Label("Zichtbaar");

		TB_title = new TextBox();
		TB_credits = new TextBox();
		TB_dag = new TextBox();
		TB_maand = new TextBox();
		TB_jaar = new TextBox();

		TA_description = new TextArea();
		CB_visible = new CheckBox();
		BTN_editAuction = new Button("Wijzig");
		BTN_editAuction.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				veiling.setCategory_id(Integer.parseInt(LB_category.getValue(LB_category.getSelectedIndex())));
				veiling.setTitle(TB_title.getText());
				veiling.setDescription(TA_description.getText());
				veiling.setEndDate(new Date((Integer.parseInt(TB_jaar.getText())-1900), (Integer.parseInt(TB_maand.getText())-1), Integer.parseInt(TB_dag.getText())));
				veiling.setStartprice(Integer.parseInt(TB_credits.getText()));
				veiling.setVisible(CB_visible.getValue());
				myService.saveAuction(veiling, opslaan);
			}
		});
		myService.getAuction(id, getAuction);
	}
}